package com.polaris.live.ui.main.fragment

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.polaris.live.R
import com.polaris.live.adapter.home.DynamicAdapter
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.AppConst
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.util.NavigationBarUtils
import com.polaris.live.constant.UserConst
import com.polaris.live.databinding.FragmentDynamicBinding
import com.polaris.live.popup.GlobalPopupManager
import com.polaris.live.resp.back_resp.ListResultCommunityListResp
import com.polaris.live.ui.main.live.AbstractLiveListFragment
import com.polaris.live.utils.CommonUtils.toBoolean
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.photo.PicturePreview
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.polaris.live.viewmodel.home.DynamicViewModel

/**
 * 动态列表
 *
 * @author Created by 半仙 on 2024/9/4/004 9:27
 */
class DynamicFragment : AbstractLiveListFragment<DynamicViewModel, FragmentDynamicBinding>() {

    override val refreshView by lazy { mBinding.dynamicRecycler }

    override val layoutManager by lazy { LinearLayoutManager(context) }

    override val mAdapter by lazy { DynamicAdapter() }

    override val listLiveData by lazy { mViewModel.mListLiveData }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        initObserver()
        NavigationBarUtils.setImmersionBarForMain(this, mBinding.dynamicRecycler)
    }

    private fun initObserver() {
        mViewModel.idLiveData.observe(this) { resp ->
            val item = mAdapter.getItemOrNull(resp.second) as? ListResultCommunityListResp ?: return@observe
            if (item.like.toBoolean()) {
                item.like = AppConst.NOLONG
                item.likes -= 1
            } else {
                item.like = AppConst.YESLONG
                item.likes += 1
            }
            mAdapter.notifyItemChanged(resp.second)
        }
        mViewModel.translateLiveData.observe(this) { resp ->
            val item = mAdapter.getItemOrNull(resp.second) as? ListResultCommunityListResp ?: return@observe
            item.translate = resp.first.text
            mAdapter.notifyItemChanged(resp.second)
        }
    }

    override fun initListener() {
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            val item =
                mAdapter.getItemOrNull(position) as? ListResultCommunityListResp ?: return@setOnItemChildClickListener
            GlobalPopupManager.showCompletePopup(requireContext(), isOk = {
                when (view.id) {
                    R.id.ll_head -> {
                        Router.builder(RoutePath.USER_SPACE_ACTIVITY)
                            .withInt("page", UserConst.From.COMMUNITY)
                            .withLong("userId", item.userId)
                            .navigation(requireActivity())
                    }

                    R.id.iv_say_hi -> {
                        Router.builder(RoutePath.MESSAGE_ACTIVITY)
                            .withInt("page", UserConst.From.COMMUNITY)
                            .withString("nickname", item.nickName)
                            .withString("avatar", item.avatar)
                            .withLong("userId", item.userId)
                            .navigation()
                    }

                    R.id.iv_user_img -> {
                        item.picUrl.let {
                            PicturePreview.with(requireActivity())
                                .byView(view)
                                .setImageUrlList(ArrayList(listOf(ImageUtils.imgPath(item.picUrl))))
                                .go()
                        }
                    }

                    R.id.iv_nice -> {
                        mViewModel.communityLike(item.communityId, position)
                    }

                    R.id.tv_content -> {
                        if (item.translate.isNullOrEmpty()) {
                            UserManager.getUser().profile?.appLanguage?.let {
                                mViewModel.translateText(position, item.communityId, it)
                            }
                        } else {
                            item.translate = null
                            mAdapter.notifyItemChanged(position)
                        }
                    }
                }
            })
        }
        mBinding.ivReleaseDynamic.setOnSingleClickListener {
            GlobalPopupManager.showCompletePopup(requireContext(), isOk = {
                Router.builder(RoutePath.RELEASE_DYNAMIC_ACTIVITY).navigation(requireContext())
            })
        }
    }
}